package com.blackhorse.order.controller;

import com.blackhorse.order.command.OrderCommand;
import com.blackhorse.order.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/buy/{id}")
    public Product buyProductById(@PathVariable Long id) throws ExecutionException, InterruptedException {
        //原始写法：服务不隔离，出现高并发问题
//        String url = "http://localhost:9001/product/" + id;
//        Product product = restTemplate.getForObject(url, Product.class);

        //使用Hystrix 实现线程池对服务隔离
        return new OrderCommand(restTemplate, id).execute();
    }

    @GetMapping("/{id}")
    public String findOrder(@PathVariable Long id) {
        System.out.println(Thread.currentThread().getName());
        return "根据id查询订单";
    }

}
